package workflow

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"seawolf-admin-go/internal/app/admin/controller"
	"seawolf-admin-go/internal/model/workflowEntity"
	"seawolf-admin-go/internal/request/BaseReq"
	"seawolf-admin-go/internal/request/WorkflowReq"
	"seawolf-admin-go/internal/routers"
	"seawolf-admin-go/internal/service/WorkflowService"
	"seawolf-admin-go/utility/response"
)

type cProcdef struct {
	*controller.BaseController
}

func NewProcdefController(group *ghttp.RouterGroup, defaultVersion string) {
	// 初始化内部变量
	inputReq := &BaseReq.I{}
	c := &cProcdef{&controller.BaseController{Req: inputReq}}
	c.New(group, defaultVersion)
}

func (c *cProcdef) New(group *ghttp.RouterGroup, defaultVersion string) {
	// 配置表名
	c.Req = &BaseReq.I{
		TableName:          "procdef",
		InfoIgnoreProperty: "delete_at",
		SoftDeleteField:    "delete_at",
		ListQueryOp: &BaseReq.QueryOp{
			IsCache: 600,
		},
	}
	// 登录中间件
	// 权限判断中间件
	// 处理初始化方法和结束方法中间件
	group.Middleware(func(r *ghttp.Request) {
		c.Init(r)
		r.Middleware.Next()
		c.Shut(r)
	})
	// 注册基础路由
	controller.RegBase(group, c, nil)

	group.POST("/check", func(r *ghttp.Request) {
		routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
			"1.0.0": c.actionCheck,
		}, defaultVersion, routers.DemoteStrategy)
	})
}

func (c *cProcdef) Add(r *ghttp.Request) *response.JsonResponse {
	ctx := r.GetCtx()
	var req *WorkflowReq.ProcessAdd
	err := r.Parse(&req)
	if err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	fmt.Println("\n")
	fmt.Printf("%-v", &req)

	//保存
	id, err := WorkflowService.SaveProcessDefReq(ctx, req, 0)
	if err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	return response.SuccessByRequestMessageData(r, "保存成功", g.Map{"id": id})
}

func (c *cProcdef) _Update(r *ghttp.Request) *response.JsonResponse {
	ctx := r.GetCtx()
	adminId := r.GetCtxVar("admin_id", 0).String()
	fmt.Println(adminId)
	//r.Response.WriteJsonExit(AdminService.Permmenu(ctx, c.Req))
	var req *WorkflowReq.ProcessAdd
	err := r.Parse(&req)
	if err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	fmt.Println("\n")
	fmt.Printf("%-v", &req)
	//获取流程定义id
	oldProcdefId := r.GetQuery("procdef_id").Int64()
	if oldProcdefId == 0 {
		return response.FailByRequestMessage(r, "流程id不存在")
	}
	//保存
	id, err := WorkflowService.SaveProcessDefReq(ctx, req, oldProcdefId)
	if err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	return response.SuccessByRequestMessageData(r, "保存成功", g.Map{"id": id})
}

func (c *cProcdef) actionCheck(r *ghttp.Request) *response.JsonResponse {
	//ctx := r.GetCtx()
	resource := r.GetBodyString()
	fmt.Println("%s", resource)
	// 流程定义有效性检验
	var res *workflowEntity.Node
	r.Parse(&res)
	err := WorkflowService.CheckProcessConifgIsValid(res)
	if err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	fmt.Println("\n")
	fmt.Printf("%-v", &res)

	return response.SuccessByRequestMessageData(r, "解析结果", &res)
}
